package action.FrontAction;

import com.opensymphony.xwork2.ActionSupport;
import common.StringUtils;
import common.cos.FileUploadCos;
import entity.CroCustomer;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import service.CroCustomerService;

import javax.servlet.ServletContext;
import java.io.File;
import java.util.UUID;

/**
 * Created by xuweijie on 17-5-17.
 * 个人信息管理
 */
public class PersionAction extends ActionSupport {

    @Autowired
    private CroCustomerService croCustomerService;
    @Autowired
    private FileUploadCos fileUploadCos;

    private String username;
    private String sex;
    private String phone;
    private String email;
    private String successResult;
    private String oldpassword;
    private String newpassword;
    private String myphoto;
    private File photo;
    private String photoFileName;
    private String photoContentType;

    public String showMyInfo(){
        return SUCCESS;
    }

    /*
    * 更改顾客信息
    * */
    public String updatecusdata(){
        ServletContext context= ServletActionContext.getServletContext();
        CroCustomer customer=(CroCustomer) context.getAttribute("customer");
        CroCustomer croCustomer=croCustomerService.FindCustomerById(customer.getId());
        croCustomer.setUsername(username);
        croCustomer.setSex(sex);
        croCustomer.setPhone(phone);
        croCustomer.setEmail(email);
        if(StringUtils.isNotBlank(myphoto)){
            croCustomer.setPhoto(myphoto);
        }
        if(croCustomerService.updateCustomer(croCustomer)){
            context.setAttribute("customer",croCustomer);
        }
        return SUCCESS;
    }

    /*
    * 显示修改密码页面
    * */
    public String showupdatepwd(){
        return "updatepwd";
    }

    /*
    * 顾客修改密码
    * */
    public String updatepwd(){
        ServletContext context= ServletActionContext.getServletContext();
        CroCustomer customer=(CroCustomer) context.getAttribute("customer");
        if((new Md5Hash(oldpassword,customer.getSalt()).toString()).equals(customer.getPasswd())){
            String salt= UUID.randomUUID().toString().substring(0,8);
            customer.setPasswd(new Md5Hash(newpassword,salt).toString());
            customer.setSalt(salt);
            if(croCustomerService.updateCustomer(customer)){
                context.removeAttribute("customer");
                successResult="0";//密码修改成功
            }
        }else {
            successResult="1";//旧密码验证不匹配
        }
        return SUCCESS;
    }

    /**
     * 上传图片
     * @return
     */
    public String uploadphoto(){
        successResult=fileUploadCos.uploadFile(photo.getAbsolutePath(),photoFileName);
        return SUCCESS;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getSuccessResult() {
        return successResult;
    }

    public void setSuccessResult(String successResult) {
        this.successResult = successResult;
    }

    public String getOldpassword() {
        return oldpassword;
    }

    public void setOldpassword(String oldpassword) {
        this.oldpassword = oldpassword;
    }

    public String getNewpassword() {
        return newpassword;
    }

    public void setNewpassword(String newpassword) {
        this.newpassword = newpassword;
    }

    public String getMyphoto() {
        return myphoto;
    }

    public void setMyphoto(String myphoto) {
        this.myphoto = myphoto;
    }

    public File getPhoto() {
        return photo;
    }

    public void setPhoto(File photo) {
        this.photo = photo;
    }

    public String getPhotoFileName() {
        return photoFileName;
    }

    public void setPhotoFileName(String photoFileName) {
        this.photoFileName = photoFileName;
    }

    public String getPhotoContentType() {
        return photoContentType;
    }

    public void setPhotoContentType(String photoContentType) {
        this.photoContentType = photoContentType;
    }
}
